﻿#region Copyrights Notice
//©2009 Microsoft Corporation.  All rights reserved.
//This portal design is based on the design and some code from DropThings.com which is also licensed under an MS-PL license.
#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;


namespace Microsoft.InformationSecurity.RiskManagement.ISRMWeb
{
    public partial class MenuControl1 : System.Web.UI.UserControl
    {
 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.ClientScript.IsClientScriptBlockRegistered("siteBase"))
            {
                if (Request.ApplicationPath.ToString().Equals("/"))
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "siteBase", string.Format("var siteBase = '/'; var themeName = '{0}'", Page.Theme.ToString()), true);
                else
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "siteBase", string.Format("var siteBase = '{0}/'; var themeName = '{1}'", Request.ApplicationPath, Page.Theme.ToString()), true);
            }
            if (!Page.ClientScript.IsClientScriptBlockRegistered("navigationScript"))
            {
                if (!Request.ApplicationPath.ToString().Equals("/"))
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "navigationScript", string.Format("<script src='{0}/scripts/Navigation.js' type='text/javascript'></script>", Request.ApplicationPath.ToString()));      
                else
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "navigationScript", "<script src='/scripts/Navigation.js' type='text/javascript'></script>");      
            }
        }
        protected void Page_UnLoad(object sender, EventArgs e)
        {
        }

        protected override void OnInit(EventArgs e)
        {
            StringBuilder MenuList = new StringBuilder();
            SiteMapNode root = SiteMap.RootNode;

            if (SiteMap.RootNode.HasChildNodes)
            {
                IEnumerator rootNodesChildrenEnumerator = SiteMap.RootNode.ChildNodes.GetEnumerator();
                while (rootNodesChildrenEnumerator.MoveNext())
                {
                    SiteMapNode current = (SiteMapNode)rootNodesChildrenEnumerator.Current;
                   
                        MenuList.Append(current.ToString());
                        MenuList.Append(",");
                        MenuList.Append(current.Description);
                        MenuList.Append(",");
                        MenuList.Append(current.Url);

                        if (((SiteMapNode)rootNodesChildrenEnumerator.Current).HasChildNodes)
                        {
                            IEnumerator childNodeChildrenEnumerator = ((SiteMapNode)(rootNodesChildrenEnumerator.Current)).ChildNodes.GetEnumerator();
                            while (childNodeChildrenEnumerator.MoveNext())
                            {
                                SiteMapNode childCurrent = (SiteMapNode)childNodeChildrenEnumerator.Current;
                                bool blnAllow = false;
                                foreach (string roleName in childCurrent.Roles)
                                {
                                    if (roleName.Equals("*"))
                                    {
                                        blnAllow = true;
                                        break;
                                    }
                                    else if (this.Page.User.IsInRole(roleName))
                                    {
                                        blnAllow = true;
                                        break;
                                    }
                                }
                                if (blnAllow)
                                {
                                    MenuList.Append("&"); //Menu Item seperator
                                    MenuList.Append(childCurrent.ToString()); //attribute attribute seperator
                                    MenuList.Append(",");
                                    MenuList.Append(childCurrent.Description); //attribute seperator
                                    MenuList.Append(",");
                                    MenuList.Append(childCurrent.Url); //Menu Item seperator
                                }
                            }
                        }
                        MenuList.Append(";"); //Menu Level seperator 
                }
            }
            //Set the string as input value text to pass on to javascript.
            MenuItemsInput.Value = MenuList.ToString();
        }
    }
}